<%@ page language="java" contentType="text/html; charset=utf-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="okhttp3.*" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.util.concurrent.TimeUnit" %>
<%
    String requestid = Util.null2String(Util.null2String(request.getParameter("requestid")));
    String tablename = "formtable_main_86";
    out.println(">>>>>testCreateGys.jsp>>>>>requestid=" + requestid + "<br/>");
    try {
        JSONArray paramsList = new JSONArray();
        rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
        rs.next();
        String bm = Util.null2String(rs.getString("bm"));
        String mc = Util.null2String(rs.getString("mc"));
        String jc = Util.null2String(rs.getString("jc"));
        String lxr = Util.null2String(rs.getString("lxr"));
        String yx = Util.null2String(rs.getString("yx"));
        String dz = Util.null2String(rs.getString("dz"));
        String gsh = Util.null2String(rs.getString("gsh"));
        String shengxrq = Util.null2String(rs.getString("shengxrq"));
        String sxrq = Util.null2String(rs.getString("sxrq"));
        String fldanhang = Util.null2String(rs.getString("fldanhang"));
        String jybzdx = Util.null2String(rs.getString("jybzdx"));
        String skbzdx = Util.null2String(rs.getString("skbzdx"));
        String szhdx = Util.null2String(rs.getString("szhdx"));
        JSONObject paramsJson = new JSONObject();
        JSONObject contextdtoJson = new JSONObject();
        contextdtoJson.put("EntCode", bb.getPropValue("u9", "entcode"));
        contextdtoJson.put("OrgCode", bb.getPropValue("u9", "orgcode"));
        contextdtoJson.put("UserCode", bb.getPropValue("u9", "userCode"));
        paramsJson.put("ContextDTO", contextdtoJson);
        paramsJson.put("OtherID", bb.getPropValue("u9", "clientId"));
        paramsJson.put("Code", bm);
        paramsJson.put("Name", mc);
        paramsJson.put("ShortName", jc);
        paramsJson.put("StateTaxNo", gsh);
        JSONObject effectiveJson = new JSONObject();
        effectiveJson.put("IsEffective", true);
        effectiveJson.put("EffectiveDate", shengxrq);
        effectiveJson.put("DisableDate", sxrq);
        paramsJson.put("Effective", effectiveJson);
        JSONObject categoryJson = new JSONObject();
        categoryJson.put("Code", fldanhang);
        paramsJson.put("Category", categoryJson);
        JSONObject tradecurrencyJson = new JSONObject();
        tradecurrencyJson.put("Code", jybzdx);
        paramsJson.put("TradeCurrency", tradecurrencyJson);
        JSONObject checkcurrencyJson = new JSONObject();
        checkcurrencyJson.put("Code", skbzdx);
        paramsJson.put("CheckCurrency", checkcurrencyJson);
        JSONObject taxscheduleJson = new JSONObject();
        taxscheduleJson.put("Code", szhdx);
        paramsJson.put("TaxSchedule", taxscheduleJson);
        paramsList.add(paramsJson);
        JSONObject resultJson = CreateSupplier(paramsList);
        out.println(">>>>>WeaFwsqlcAction>>>>>resultJson=" + resultJson);
    } catch (Exception e) {
        out.println(">>>>>WeaFwsqlcAction>>>>>创建文档归档信息包异常:" + e);
    }

%>
<%!

    private static BaseBean baseBean = new BaseBean();

    /**
     * 供应商创建
     *
     * @param paramsList
     * @return
     */
    public JSONObject CreateSupplier(JSONArray paramsList) {
        JSONObject returnJson = new JSONObject();
        String code = "fail";
        String message = "";
        String data = "";
        String baseUrl = Util.null2String(baseBean.getPropValue("u9", "url"));
        String apiUrl = "/u9c/webapi/CustSupplier/CreateSupplier";
        JSONObject resultJson = sendOKHttpPostRequest(baseUrl + apiUrl, paramsList.toJSONString());
        baseBean.writeLog(">>>>>getAuthLogin>>>>>>resultJson=" + resultJson);
        message = Util.null2String(resultJson.getString("message"));
        if ("success".equals(message)) {
            JSONObject dataJson = resultJson.getJSONObject("data");
            String ResCode = Util.null2String(dataJson.getString("ResCode"));
            if ("0".equals(ResCode)) {
                JSONArray resultData = dataJson.getJSONArray("Data");
                String IsSucess = resultData.getJSONObject(0).getString("IsSucess");
                if ("true".equals(IsSucess)) {
                    code = "success";
                    data = resultData.getJSONObject(0).getString("ID");
                } else {
                    message = resultData.getJSONObject(0).getString("ErrorMsg");
                }
            }
        }
        returnJson.put("code", code);
        returnJson.put("data", data);
        returnJson.put("message", message);
        return returnJson;
    }

    /**
     * 获取token
     *
     * @return
     */
    private String getAuthLogin() {
        String token = "";
        String apiUrl = "/U9C/webapi/OAuth2/AuthLogin";
        String baseUrl = Util.null2String(baseBean.getPropValue("u9", "url"));
        String userCode = Util.null2String(baseBean.getPropValue("u9", "userCode"));
        String entcode = Util.null2String(baseBean.getPropValue("u9", "entcode"));
        String orgcode = Util.null2String(baseBean.getPropValue("u9", "orgcode"));
        String clientId = Util.null2String(baseBean.getPropValue("u9", "clientId"));
        String clientSecret = Util.null2String(baseBean.getPropValue("u9", "clientSecret"));
        String url = baseUrl + apiUrl + "?userCode=" + userCode + "&entcode" + entcode + "&orgcode=" + orgcode + "&clientid=" + clientId + "&clientsecret=" + clientSecret;
        JSONObject resultJson = sendOKHttpGetRequest(url);
        baseBean.writeLog(">>>>>getAuthLogin>>>>>>resultJson=" + resultJson);
        String message = Util.null2String(resultJson.getString("message"));
        if ("success".equals(message)) {
            JSONObject dataJson = resultJson.getJSONObject("data");
            String ResCode = Util.null2String(dataJson.getString("ResCode"));
            if ("0".equals(ResCode)) {
                token = Util.null2String(dataJson.getString("Data"));
            }
        }
        return token;
    }

    /**
     * get请求
     *
     * @param apiUrl
     * @return
     */
    private JSONObject sendOKHttpGetRequest(String apiUrl) {
        baseBean.writeLog(">>>>>sendOKHttpGetRequest>>>>>>apiUrl=" + apiUrl);
        JSONObject resultJson = new JSONObject();
        String message = "";
        String data = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            Request request = new Request.Builder()
                    .url(apiUrl)
                    .get()
                    .build();
            Response response = client.newCall(request).execute();
            String result = Util.null2String(response.body().string());
            if (!"".equals(result)) {
                data = result;
                message = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            e.printStackTrace();
        }
        resultJson.put("data", data);
        resultJson.put("message", message);
        return resultJson;
    }

    /**
     * Post请求
     *
     * @param params
     * @param apiUrl
     * @return
     */
    public JSONObject sendOKHttpPostRequest(String apiUrl, String params) {
        String baseUrl = Util.null2String(baseBean.getPropValue("u9", "url"));
        String token = getAuthLogin();
        JSONObject resultJson = new JSONObject();
        String message = "";
        String data = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, params);
            Request request = new Request.Builder()
                    .url(baseUrl + apiUrl)
                    .method("POST", body)
                    .addHeader("token", token)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();
            String result = Util.null2String(response.body().string());
            if (!"".equals(result)) {
                data = result;
                message = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            e.printStackTrace();
        }
        resultJson.put("data", data);
        resultJson.put("message", message);
        return resultJson;
    }

%>